package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;

import static android.telecom.DisconnectCause.LOCAL;

/**
 * Created by XXX on 2017/5/23.
 */

public class VideoAdapter extends BaseAdapter{
    private Context context;
    private List<String> list;

    public VideoAdapter( Context context, List<String> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.myvideo_info,parent,false);
            holder=new ViewHolder();
            holder.myVideoTime= (TextView) convertView.findViewById(R.id.myVideoTime);
           convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        final String filename=list.get(position);
        String[] name=filename.split("/");
        String[] thisname=name[name.length-1].split("_");
        holder.myVideoTime.setText(thisname[2]+thisname[3]);

        return convertView;
    }

    class ViewHolder{
       TextView myVideoTime;
    }


    public long getDurationLong(String url,int type){
        String duration = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            //如果是网络路径
            if(type == 1){
                retriever.setDataSource(url,new HashMap<String, String>());
            }else if(type == LOCAL){//如果是本地路径
                retriever.setDataSource(url);
            }
            duration = retriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
            Log.e("nihao","duration"+ duration);
        } catch (Exception ex) {
            Log.e("nihao", "获取音频时长失败");
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
                Log.e("nihao", "释放MediaMetadataRetriever资源失败");
            }
        }
        if(!TextUtils.isEmpty(duration)){
            return Long.parseLong(duration);
        }else{
            return 0;
        }
    }
}
